package com.cms.admin.core.http;

/**
 * Created by YYY-Work on 2017/5/25.
 */
import java.util.HashMap;
import java.util.Map;

import com.cms.admin.util.PropertiesService;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.Consts;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;


public class HttpClientHelper {
    private static Log log = LogFactory.getLog(HttpClientHelper.class);

    @SuppressWarnings("deprecation")
    public static String sendPostHttp(String url, String message) {

        String s = "";
        if (null == message || "".equals(message)) {
            log.info("a blank message, return.");
            return "a blank message, return.";
        }
        log.info(message);
        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, 10 * 1000);
        HttpConnectionParams.setSoTimeout(httpParameters, 10 * 1000);
        HttpConnectionParams.setSocketBufferSize(httpParameters, 8192);
        HttpConnectionParams.setSoKeepalive(httpParameters, true);

        HttpClient httpClient = new DefaultHttpClient(httpParameters);

        HttpPost post = new HttpPost(url);

        StringEntity entity = new StringEntity(message, ContentType.create(
                "plain/text", Consts.UTF_8));

        post.setEntity(entity);

        int retry = 0;

        HttpResponse response = null;

        try {
            response = httpClient.execute(post);

            if (response.getStatusLine().getStatusCode() == 200) {
                s = EntityUtils.toString(response.getEntity());
                log.info(s);
            } else {
                s = EntityUtils.toString(response.getEntity());
                log.info(s);
            }



        } catch (Exception e) {
            String error=e.getMessage();
            s = "{\"retCode\":\"1002\",\"retInfo\":\""+error+"\"}";
            e.printStackTrace();
        }
        return s;
    }

    @SuppressWarnings("deprecation")
    public static String sendPostHttp( String message) {
        return sendPostHttp(PropertiesService.getString("serverPostUrl").trim(),message);
    }
    
    
    public static void main(String[] args) {
    //	String message ="specimenId=13062353";
    	Map map = new HashMap();
    	
    	map.put("specimenId", 13062353);
    	
    	String message = JSONObject.fromObject(map).toString();
    	System.out.println(message);
    	
    	System.out.println(sendPostHttp("http://127.0.0.1:8080/AppInterface/servlet/QueryHospitalSpecimen",message));    	
    	
		
	}

}